package invoice;

import invoice.line.EnglishLine;
import invoice.line.Line;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.LinkedList;

public class FileController {
    private String item;
    private int units;
    private double unitPrice;
    private File invoice_file=new File("e:\\shopping.txt");

    public File getInvoice_file() {
        return invoice_file;
    }

    public void setInvoice_file(String f){
        this.invoice_file=new File(f);
    }

    public void setInvoice_file(File invoice_file) {
        this.invoice_file = invoice_file;
    }

    public Line[] addLine(){
        LinkedList<Line> lineList=new LinkedList<>();
        FileReader fr=null;
        BufferedReader br=null;

        if(!invoice_file.exists()){
           System.out.println("文件不存在");
           System.exit(0);
        }else{
            invoice_file=invoice_file;
            try {
                fr = new FileReader(invoice_file);
                br = new BufferedReader(fr);
                String line ;
                String[] splitLine;
                int listNum=0;
                while ((line = br.readLine()) != null) {
                    splitLine = line.split("\\s+");
                    item = splitLine[0];
                    units = Integer.valueOf(splitLine[1]);
                    unitPrice = Double.valueOf(splitLine[2]);
                    lineList.add(new EnglishLine(item,units,unitPrice));
                    listNum++;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return lineList.toArray(new Line[lineList.size()]);
    }
}
